var express = require('express');
var router = express.Router();
var { getInventoryList, updateInventory, addInventory, removeInventory, quireInventory, getInventoryById, inquirecommodity, getcommodity, submitcommodity, addcommodity, deltcommodity, updatecommodity, getAllInventory } = require("../service/merchantService")
var {
    uploadFiles,
    moveFiles,
    removeFiles, } = require("../utils/uploadFiles");
router.get("/getInventoryList", async function (req, res, next) {
    var data = req.query;
    
    res.send(await getInventoryList(data));
})
router.get("/getAllInventory", async function (req, res, next) {
    var data = req.query;
    res.send(await getAllInventory(data));
})
//获取商品
router.get("/getcommodity", async function (req, res, next) {

    var data = req.query;
    res.send(await getcommodity(data));
})
//新增商品
router.post('/addcommodity', async function (req, res, next) {
    res.send(await addcommodity(req.body))
})
//删除商品
router.get('/deltcommodity', async function (req, res, next) {
    res.send(await deltcommodity(req.query))
})
// 显示修改商品
router.get('/updatecommodity', async function (req, res, next) {
    res.send(await updatecommodity(req.query))
})
//修改商品
router.post('/submitcommodity', async function (req, res, next) {
    // var filename=req.body.image
    // moveFiles({
    //     fromPath: './public/temp',//源文件路径
    //     toPath: './public/images',//移动的新路径
    //     filename
    //   });
    //   removeFiles('./public/temp')
    res.send(await submitcommodity(req.body))
})
//引用方法上传图片
var images = uploadFiles({
    path: './public/images',//图片上传成功后存储路径
    key: 'file'//key值必须与前端的那个值一样
})
//引用方法上传图片
var imagess = uploadFiles({
    path: './public/detailsImages',//图片上传成功后存储路径
    key: 'file'//key值必须与前端的那个值一样
})
//图片详情上传

router.post('/imgs', imagess, async function (req, res, next) {
    var img = [];
    img.push(req.files[0].filename);
    res.send(img)
    // res.send({ img })
})
//商品图片上传
router.post('/img', images, async function (req, res, next) {
    var img = req.files[0].filename;
    res.send({ img })
})
// 修改库存
router.post("/updateInventory", async function (req, res, next) {
    var data = req.body;
    res.send(await updateInventory(data));
})
// 添加库存（暂未使用）
router.post("/addInventory", async function (req, res, next) {
    var data = req.body;
    if (data.img) {
        moveFiles({
            fromPath: './public/addImg/',
            toPath: "./public/images/",
            filename: data.img,
        });
        // data.img="images/"+data.img;
        removeFiles("./public/addImg/");
    } else {
        data.img = "暂无图片.jpg";
    }
    // 循环将详情图片添加至指定目录
    if (data.particularsImg.length > 0) {
        data.particularsImg.some((item,index) => {
            item=item.split("/")[1];
            moveFiles({
                fromPath: './public/updateImg/',
                toPath: "./public/detailsImages/",
                filename: item,
            });
            data.particularsImg[index]="detailsImages/"+item;
        })
        removeFiles("./public/updateImg/");
    }
    var tf = await addInventory({ ...data });
    if (tf) {
        res.send(true);
    } else {
        res.send(false)
    }
});
// 库存删除
router.get("/removeInventory", async function (req, res, next) {
    var data = await removeInventory(req.query)
    if (data.deletedCount == 1) {
        res.send(true);
    } else {
        res.send(false)
    }

})
// 通过id查询库存
router.get("/getInventoryById", async function (req, res, next) {
    var data = req.query;
    res.send(await getInventoryById(data))
})

// 图片的保存
var updateImg = uploadFiles({
    path: './public/updateImg',
    key: "file",
});
var addImg = uploadFiles({
    path: './public/addImg',
    key: "file",
});
// 获取图片选择的图片的名称
router.post('/uploadImgs', updateImg, async function (req, res, next) {
    var data = (req.files).filter((item) => {
        return item.size < (1024 * 1024 * 2)
    });
    var imgName = data.map((item, index) => { return "updateImg/"+item.filename });
    res.send(imgName);

})
// 获取图片选择的图片的名称
router.post('/addImgs', addImg, async function (req, res, next) {
    var data = (req.files).filter((item) => {
        return item.size < (1024 * 1024 * 2)
    });
    var imgName = data.map((item, index) => { return item.filename });
    res.send(imgName);
})
router.post("/quireInventory", async function (req, res, next) {
    var data = req.body;
    res.send(await quireInventory(data));
})
module.exports = router;